//
//  ReturnGoodsVC.m
//  代理端
//
//  Created by sliver on 15/12/15.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "ReturnGoodsVC.h"
#import "Unit.h"
#import "WXDataService.h"
@interface ReturnGoodsVC ()<UITextFieldDelegate,UITextViewDelegate,UIAlertViewDelegate,BackViewDeleget>
{
    UITextField  *numTextField; //金额
    UITextView   *reasonTextview;//原因
    
    UILabel *reasonlab;

}
@end

@implementation ReturnGoodsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNav];
    [self loadmainView];
    
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ceshi) name:UITextViewTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ceshi2) name:UITextViewTextDidEndEditingNotification object:nil];

}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidBeginEditingNotification object:nil];
    
    
}
-(void)loadmainView{
    //输入手机号TextField
    numTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 150,ScreenW - 80, 40)];
    numTextField.delegate = self;
    numTextField.backgroundColor = [UIColor whiteColor];
    numTextField.font = [UIFont systemFontOfSize:16.0];
    numTextField.placeholder = @" 请输入退货数量";
    [numTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    numTextField.textAlignment = NSTextAlignmentLeft;
    numTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    numTextField.keyboardType = UIKeyboardTypeNumberPad;
    numTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    numTextField.returnKeyType = UIReturnKeyDefault;
    [self.view addSubview:numTextField];
    //输入原因TextField
    reasonTextview = [[UITextView alloc]initWithFrame:CGRectMake(40, 200, ScreenW-80, 100)];
    reasonTextview.delegate = self;
    reasonTextview.textColor = [UIColor grayColor];
    reasonTextview.font = [UIFont systemFontOfSize:16];
    reasonTextview.textAlignment = NSTextAlignmentLeft;
    reasonTextview.keyboardType = UIKeyboardTypeDefault;
    reasonTextview.backgroundColor = [UIColor whiteColor];
    reasonTextview.returnKeyType = UIReturnKeyDone;
    reasonTextview.layer.masksToBounds = YES;
    reasonTextview.layer.cornerRadius = 5;
    [self.view addSubview:reasonTextview];
    
    //原因
    reasonlab = [[UILabel alloc]initWithFrame:CGRectMake(40, 205, ScreenW-80, 20)];
    reasonlab.text = @" 请输入退货原因 :";
    reasonlab.font = [UIFont systemFontOfSize:16];
    reasonlab.textColor = [UIColor grayColor];
    reasonlab.backgroundColor = [UIColor clearColor];
    [self.view addSubview:reasonlab];

    //登陆按钮
    UIButton *landBtn = [[UIButton alloc]initWithFrame:CGRectMake(60, ScreenH - 100, ScreenW - 120, 50)];
    landBtn.backgroundColor = DRINK_RED_COLOR;
    landBtn.layer.masksToBounds = YES;
    landBtn.layer.cornerRadius = 5;
    [landBtn setTitle:@"提交申请" forState:UIControlStateNormal];
    landBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [landBtn addTarget:self action:@selector(clickLand:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:landBtn];
    
}
#pragma mark - 提交
-(void)clickLand:(UIButton *)senfer{
    
    if (numTextField.text.length==0||reasonTextview.text.length==0) {
        UIAlertView *alter =  [[UIAlertView alloc]initWithTitle:@"提示" message:@"请完善退货信息" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alter show];
        return;
    }
    
    if ([self.productCount intValue]<[numTextField.text intValue]) {
        [BackView addToview:self.view WithDeleget:self WithMessage:@"申请退货数量超过订购数量" WithBtnCount:1];
        return;
    }
    else
    {
        [MyMbd addHudToView:self.view];
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:_order_id forKey:@"id_"];
        [params setObject:numTextField.text forKey:@"return_count"];
        [params setObject:reasonTextview.text forKey:@"return_goods_reason"];
        [WXDataService requestWithURL:@"mobile/agency/applyReturnGoods.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
            BOOL suc =[success boolValue];
            if (suc == 0 ) {
                UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [deleAlertView show];
                return;
            }else if(suc == 1){
//                UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//                [deleAlertView show];
                [MyMbd hidenHudFromView:self.view];

                [BackView addToview:self.view WithDeleget:self WithMessage:@"申请退货成功" WithBtnCount:1];
                
            }
        }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [numTextField resignFirstResponder];
    
    return YES;
    
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [numTextField resignFirstResponder];
    [reasonTextview resignFirstResponder];
}

#pragma mark - nav
-(void)loadNav{
    self.view.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 20)];
    redview.backgroundColor = DRINK_RED_COLOR;
    [self.view addSubview:redview];
    UILabel *redLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenW, 40)];
    redLab.backgroundColor = DRINK_RED_COLOR;
    redLab.text = @"申请退货";
    redLab.textColor = [UIColor whiteColor];
    redLab.textAlignment =  NSTextAlignmentCenter;
    redLab.font = [UIFont boldSystemFontOfSize:19];
    [self.view addSubview:redLab];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20,44, 40)];
    backBtn.adjustsImageWhenHighlighted = NO;
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
}
-(void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    reasonlab.text= @"";
    return YES;
}
-(void)ceshi
{
    reasonlab.text= @"";

}
-(void)ceshi2
{
    if (reasonTextview.text.length==0) {
        reasonlab.text= @"请输入退货原因";

    }else
    {
        reasonlab.text= @"";

    }
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if ([alertView.message isEqualToString:@"请完善退货信息"]) {
//        return;
//    }
//    
//    
//    [self dismissViewControllerAnimated:YES completion:nil];
//
//}
-(void)makeSureButtenClic:(UIButton *)btn withMessage:(NSString *)message
{
    if ([message isEqualToString:@"申请退货成功"]) {
        if (btn.tag==0) {
            
            if (self.isfromone) {
                self.block();
                [self dismissViewControllerAnimated:YES completion:nil];

            }else
            {
                [self dismissViewControllerAnimated:YES completion:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"quxiao" object:nil];
            
            }
            
            
            
//            if ([self.deleget respondsToSelector:@selector(dismessVc)]) {
//                [self.deleget dismessVc];
//            }
//            self.block();

        }
    }

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
